<?php
/**
 * -----------------------------------------------------
 *
 * models Subscribe_model
 *
 * @package     models
 * @subpackage  
 * @category    Subscribe_model  -  预约模型类
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Subscribe_model extends CI_Model
{    
	public static $error;
	const TABLE = 'xc_subscribes'; // 表名

	public function __construct()
	{
		parent::__construct();
        $this->load->database();
	}

	/**
	 * 增加数据
	 */
	public function insert($data)
	{
		if($this->db->insert(self::TABLE,$data)){
			return true;
		}else{
			return false;
		}
	}

             
	/**
	 * 查询所有的数据，并且分页
	 */
	public function get_all_data_and_page($limit,$product=NULL,$status=NULL)
	{

		$sql = 'select a.*,b.cate_name
		from '.self::TABLE.' as a left join xc_category as b
		on a.cate_id=b.cate_id 
		order by a.id desc '.$limit;
		$query=$this->db->query($sql);
		if($query->num_rows()){
			return  $query->result_array();
		}
	}

	public function get_all_data_and_page_by_where($limit,$product=NULL,$status=NULL)
	{

		if($product && $product != 'all'){
			$this->db->where('pro_id',$product);
		}		
		if($status && $status != 'all'){
			$this->db->where('status',$status);
		}	
		$this->db->select('xc_subscribes.*,xc_category.cate_name');
		$this->db->join('xc_category', 'xc_subscribes.cate_id = xc_category.cate_id');
		$this->db->limit($limit);
		$query = $this->db->get(self::TABLE);
		if($query->num_rows()){
			return  $query->result_array();
		}
	}


	/**
	 * 查询共有多少数据
	 */
	public function get_num_rows($product=NULL,$status=NULL)
	{
		
		if(!empty($product) && $product != 'all'){

			$this->db->where('pro_id',$product);
		}		
		if(!empty($status) && $status != 'all'){
			$this->db->where('status',$status);
		}	
		$query = $this->db->get(self::TABLE);
		
		return $query->num_rows();
	}

	/**
	 * 编辑数据信息
	 */
	public function update($data,$id)
	{
		if($this->db->update(self::TABLE,$data,'id = '.$id)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 根据id查询数据信息
	 */
	public function get_a_data_by_id($aid)
	{
		$this->db->where('id',$aid);
		$query = $this->db->get(self::TABLE);
		return $query->row_array();
	}

	/**
	 * 根据id删除数据
	 */
	public function delete($id)
	{
		$this->db->where('id',$id);
		if($this->db->delete(self::TABLE)){
			return true;
		}else{
			return false;
		}
	}

}